package com.zs.grain.ui

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.xnkj1688.semplibrary.mvvm.BaseViewModel
import com.zs.grain.service.*
import com.xnkj1688.semplibrary.entity.Ingrident
import com.xnkj1688.semplibrary.entity.Receipe
import io.reactivex.Single
import io.reactivex.schedulers.Schedulers

import javax.inject.Inject

/**
 * Created by Wesley on 2017/7/17.
 */

class ReceipeViewModel @Inject constructor(
    val mApp: Application,
    val mDeviceService: DeviceService,
    val mDataService: DataService
) : BaseViewModel(mApp) {

    val productViewModels = MutableLiveData<MutableList<ProductItemViewModel>>()
    val receipeViewModels = MutableLiveData<MutableList<ReceipeItemViewModel>>()

    private var currentProductId = 0L

    init {
        var products = mDataService.goods
        var list = ArrayList<ProductItemViewModel>()
        for (item in products) {
            var model = ProductItemViewModel(mApp)
            model.id = item.id
            model.name = item.name
            list.add(model)
        }
        productViewModels.value = list
        var test = ProductItemViewModel(mApp)
        test.id = 1001
        test.name = "花纤芸豆"
        onClickProduct(test)
    }

    fun onClickProduct(vm: ProductItemViewModel) {
        currentProductId = vm.id
        receipeViewModels.value = this.getReceipeViewModels(currentProductId)
    }

    fun onClickReceipe(vm: ReceipeItemViewModel) {
        var notify = this.getNotify("AdjustReceipe")
        notify.putLong("ReceipeId", vm.id)
        this.uiNotify.value = notify
    }

    fun onClickTest() {
        if (currentProductId.toInt() != 0) {
            Single.just(1)
                .subscribeOn(Schedulers.io())
                .subscribe({
                    this.uiNotify.postValue(this.getNotify("ShowProgress"))
                    mDataService.insertLog("Operation:测试制作产品:"+currentProductId.toString())
                    mDeviceService.makeProduct(currentProductId)
                    this.uiNotify.postValue(this.getNotify("HideProgress"))
                }, {
                    it.printStackTrace()
                })
        } else {
            this.uiNotify.postValue(this.getNotify("ShowToast"))
        }
    }

    fun getReceipe(id: Long): Receipe? {
        return mDataService.getReceipe(id)
    }

    fun getIngrident(id: Long): Ingrident? {
        return mDataService.getIngrident(id)
    }

    fun changeValue(id: Long, value: Int) {
        var receipe = mDataService.getReceipe(id)
        if (receipe != null) {
            receipe.std = value
            mDataService.DaoReceipe.update(receipe)
        }
        receipeViewModels.value = this.getReceipeViewModels(currentProductId)
    }

    fun getReceipeViewModels(productId: Long): ArrayList<ReceipeItemViewModel> {
        var receipes = mDataService.getReceipes(productId)
        var list = ArrayList<ReceipeItemViewModel>()
        for (item in receipes) {
            var model = ReceipeItemViewModel(mApp)
            model.id = item.id
            model.ingridentId = item.ingridentId
            val ingrident = mDataService.getIngrident(item.ingridentId.toLong())
            if (ingrident != null) model.ingridentName = ingrident.name
            var components = mDataService.getComponents(item.ingridentId)
            if (components.size > 0) model.componentName = components[0].name
            model.count = item.std
            list.add(model)
        }
        return list
    }
}
